#ifndef EDITOR_H_INCLUDED
#define EDITOR_H_INCLUDED

#include "SDL_mouse.h"

#include "Map.h"
#include "Artist.h"
#include "StateMachine.h"
#include "Camera.h"
#include "EditBar.h"
#include <sstream>

struct Coord
{
    int x;
    int y;
};

class Editor
{
    public:
        Editor(Artist* artistPointer, StateMachine* statePointer);
        void run();

    private:
        StateMachine* stateMachine;
        Artist* artist;
        Map map;
        Camera camera;
        EditBar* bar;

        int fg_select, bg_select;

        bool drawing;
        bool preview;

        SDL_Surface* blueprint;
        SDL_Surface* yellowgraph;

        SDL_Event event;

        std::vector<Coord> outline;

        void check_events();
        void update();
        void display();

        void display_tiles(SDL_Surface* screen);

        void createDefaultMap();

        void saveMap(std::string filepath = "data/maps/custom.map");

        void updateCamera();

        void validateCamera();

        void displayBlueprint(SDL_Surface* screen);

        void blitTile(SDL_Surface* screen, Tile* tile);

        void simplifyTiles();

        void drawOutline(SDL_Surface* screen);

        void addPoint(int curs_x, int curs_y);

        void interpretPoints(Direction& dir, int place);

        void tileOutline(Tile* tile);

        bool crosses(Coord point, Coord start, Coord end);

        bool pointInside(Coord point);

        bool pointAt(int x, int y);

        void closeOutline();

        void tileCycle(bool rightClick);

        void brushAndErase();

        void selectObject(std::string);

        void readObjectsDirectory();

        void removeSelected();
        void rotateSelected(Direction);

        void nudgeSelectionBG(Direction, int amount);
        void nudgeSelectionFG(Direction, int amount);
        void nudgeSelection(SDL_Rect*, Direction, int);

        void resizeMap(int, int);

        int simplify(int type);

        int interpret(  int tile,
                        int topleft,
                        int top,
                        int topright,
                        int left,
                        int right,
                        int bottomleft,
                        int bottom,
                        int bottomright
                     );

        void interpretTerrain();

        SDL_Rect getTileScreenCoord(Tile* tile);
        SDL_Rect getTileFromSheet(int type);
};


#endif // EDITOR_H_INCLUDED
